<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{title}} - 图腾生命医学</title>
    <link href="/apps/css/bootstrap.min.css" rel="stylesheet">
    <link href="/apps/css/fontawesome-free-6.4.0-web/css/all.css" rel="stylesheet">
    <style>
        body { padding-top: 20px; }
        .badge { font-weight: normal; }
    </style>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h3>{{title}}</h3>
                <p>{{excerpt}}</p>
                <p>{{description}}</p>

                <div class="mb-3">
                    <a href="/apps/crm/create-sales-plan" class="btn btn-primary">
                        <i class="fas fa-plus"></i> 创建规划任务
                    </a>
                </div>

                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>名称</th>
                                <th>状态</th>
                                <th>回顾周期</th>
                                <th>计划周期</th>
                                <th>创建者</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {{#each plans}}
                            <tr>
                                <td>{{id}}</td>
                                <td>{{title}}</td>
                                <td>
                                    {{#if draft}}
                                        <span class="badge bg-secondary">未发布</span>
                                    {{/if}}
                                    {{#if published}}
                                        <span class="badge bg-success">已发布</span>
                                    {{/if}}
                                    {{#if closed}}
                                        <span class="badge bg-danger">已截收</span>
                                    {{/if}}
                                </td>
                                <td>{{review_start_date}} ~ {{review_end_date}}</td>
                                <td>{{plan_start_date}} ~ {{plan_end_date}}</td>
                                <td>{{creator_name}}</td>
                                <td>{{created_at}}</td>
                                <td>
                                    {{#if draft}}
                                        <button class="btn btn-sm btn-success me-1" onclick="updateStatus({{id}}, 'published')">
                                            <i class="fas fa-check"></i> 发布
                                        </button>
                                        <button class="btn btn-sm btn-danger" onclick="deletePlan({{id}})">
                                            <i class="fas fa-trash"></i> 删除
                                        </button>
                                    {{/if}}
                                    {{#if published}}
                                        <button class="btn btn-sm btn-danger" onclick="updateStatus({{id}}, 'closed')">
                                            <i class="fas fa-lock"></i> 截收
                                        </button>
                                    {{/if}}
                                </td>
                            </tr>
                            {{/each}}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <script src="/apps/js/jquery.min.js"></script>
    <script src="/apps/js/bootstrap.bundle.min.js"></script>
    <script>
    async function updateStatus(id, status) {
        try {
            const response = await fetch(`/apps/crm/api/sales-plans/${id}/status`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ status })
            });

            if (!response.ok) throw new Error('更新失败');

            location.reload();
        } catch (err) {
            alert('操作失败: ' + err.message);
        }
    }

    async function deletePlan(id) {
        if (!confirm('确定要删除这个规划任务吗？此操作不可恢复。')) {
            return;
        }

        try {
            const response = await fetch(`/apps/crm/api/sales-plans/${id}`, {
                method: 'DELETE',
                headers: {
                    'Content-Type': 'application/json'
                }
            });

            if (!response.ok) {
                const data = await response.json();
                throw new Error(data.error || '删除失败');
            }

            location.reload();
        } catch (err) {
            alert('删除失败: ' + err.message);
        }
    }
    </script>
</body>
</html> 